package com.example.aigc_education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.aigc_education.domain.ChatHistory;
import com.example.aigc_education.domain.Checklist;
import com.example.aigc_education.domain.vo.ChatHistoryVO;
import com.example.aigc_education.enums.ChatHistoryType;
import com.example.aigc_education.mapper.ChatHistoryMapper;
import com.example.aigc_education.mapper.ChecklistMapper;
import com.example.aigc_education.service.ChatHistoryService;
import com.example.aigc_education.service.converter.ChatHistoryConverter;
import com.example.aigc_education.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ChatHistoryServiceImpl extends ServiceImpl<ChatHistoryMapper, ChatHistory> implements ChatHistoryService {

    @Resource
    private ChatHistoryMapper chatHistoryMapper;

    @Resource
    private ChatHistoryConverter chatHistoryConverter;

    @Override
    public List<ChatHistoryVO> showAiTutorChatHistory(String botId) {
        if (StringUtils.isEmpty(botId)) {
            return new ArrayList<>();
        }

        LambdaQueryWrapper<ChatHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatHistory::getBotId, botId);
        queryWrapper.in(ChatHistory::getRole, ChatHistoryType.USER.getTitle(), ChatHistoryType.ASSISTANT.getTitle());
        queryWrapper.orderByDesc(ChatHistory::getCreateTime)
                .orderByDesc(ChatHistory::getChatId);
        List<ChatHistory> chatHistories = chatHistoryMapper.selectList(queryWrapper);

        return chatHistoryConverter.toChatHistoryVOList(chatHistories);
    }
}
